﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TI_BAL;

namespace SecuritySystem
{
    public partial class UserListing : Form
    {
        TIEntities dbContext;
        SearchUser searchUserObj = new SearchUser();


        public UserListing()
        {
            InitializeComponent();
            dbContext = new TIEntities();
        }

        private void UserListing_Load(object sender, EventArgs e)
        {
            searchUser2.buttonSearch.Click += new EventHandler(buttonSearch_Click);
            searchUser2.buttonSearchAll.Click += new EventHandler(buttonSearchAll_Click);
            
        }

        private void RefreshGrid()
        {
            try
            {

                var listOfUsers = (from suser in dbContext.SecurityUsers
                                   select new { suser.id, suser.UserId, suser.Name, suser.FatherName, suser.NIC, suser.DOB, suser.PermAddress, 
                                       suser.PresenAddress, suser.Photo, suser.ThumbImpression, suser.BarCode, suser.IsActive, 
                                       suser.PhoneNumber, suser.RoleId, suser.DOE, suser.IsResident, 
                                       suser.MembershipId, suser.RoleDesId, suser.OtherDesc, suser.ThumbImage }).ToList();
                dataGridViewUserListing.DataSource = listOfUsers;

                dataGridViewUserListing.Columns[0].Visible = false;

                dataGridViewUserListing.Columns["id"].HeaderCell.Value = "ID";
                dataGridViewUserListing.Columns["UserId"].HeaderCell.Value = "User ID";
                dataGridViewUserListing.Columns["Name"].HeaderCell.Value = "Name";
                dataGridViewUserListing.Columns["NIC"].HeaderCell.Value = "NIC";
                dataGridViewUserListing.Columns["DOB"].HeaderCell.Value = "DOB";
                dataGridViewUserListing.Columns["PermAddress"].HeaderCell.Value = "Permanent Address";
                dataGridViewUserListing.Columns["PresenAddress"].HeaderCell.Value = "Present Address";
                dataGridViewUserListing.Columns["Photo"].HeaderCell.Value = "Image";
                dataGridViewUserListing.Columns["PhoneNumber"].HeaderCell.Value = "Contact #";
                dataGridViewUserListing.Columns["FatherName"].HeaderCell.Value = "Father Name";
                dataGridViewUserListing.Columns["MembershipId"].HeaderCell.Value = "Member#";

                dataGridViewUserListing.Columns["Photo"].Visible = false;
                dataGridViewUserListing.Columns["ThumbImage"].Visible = false;
                dataGridViewUserListing.Columns["ThumbImpression"].Visible = false;
                dataGridViewUserListing.Columns["BarCode"].Visible = false;
                
                dataGridViewUserListing.Columns["RoleId"].Visible = false;
                dataGridViewUserListing.Columns["IsResident"].Visible = false;
                dataGridViewUserListing.Columns["roleDesId"].Visible = false;


            }
            catch (Exception e1)
            {


            }
        }

        private void dataGridViewUserListing_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            AddEditUser form = new AddEditUser();

            form.selectedRecord = dataGridViewUserListing.SelectedRows[0];
            form.MdiParent = this.MdiParent;

            form.Show();
            form.WindowState = FormWindowState.Minimized;
            form.WindowState = FormWindowState.Maximized;
        }

        private void searchUser2_Load(object sender, EventArgs e)
        {

        }

        private void buttonSearchAll_Click(object sender, EventArgs e)
        {
            RefreshGrid();
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            if (searchUser2.radioButtonUserDetails.Checked)
            {
                try
                {

                    var listOfUsers = (from suser in dbContext.SecurityUsers
                                       where ((suser.Name == searchUser2.textBoxName.Text)
                                       || (suser.RoleId == (int)searchUser2.comboBoxRole.SelectedValue)
                                       || (suser.RoleDesId == (int)searchUser2.comboBoxRoleDescription.SelectedValue)
                                       || (suser.NIC == searchUser2.maskedTextBoxNIC.Text)
                                       || (suser.MembershipId == searchUser2.textBoxTechNumber.Text)
                                       )
                                       select new
                                       {
                                           suser.id,
                                           suser.UserId,
                                           suser.Name,
                                           suser.NIC,
                                           suser.DOB,
                                           suser.PermAddress,
                                           suser.PresenAddress,
                                           suser.Photo,
                                           suser.ThumbImpression,
                                           suser.BarCode,
                                           suser.IsActive,
                                           suser.PhoneNumber,
                                           suser.RoleId,
                                           suser.FatherName,
                                           suser.DOE,
                                           suser.IsResident,
                                           suser.MembershipId,
                                           suser.RoleDesId,
                                           suser.OtherDesc,
                                           suser.ThumbImage
                                       }).ToList();
                    dataGridViewUserListing.DataSource = listOfUsers;

                    dataGridViewUserListing.Columns[0].Visible = false;

                    dataGridViewUserListing.Columns["id"].HeaderCell.Value = "ID";
                    dataGridViewUserListing.Columns["UserId"].HeaderCell.Value = "User ID";
                    dataGridViewUserListing.Columns["Name"].HeaderCell.Value = "Name";
                    dataGridViewUserListing.Columns["NIC"].HeaderCell.Value = "NIC";
                    dataGridViewUserListing.Columns["DOB"].HeaderCell.Value = "DOB";
                    dataGridViewUserListing.Columns["PermAddress"].HeaderCell.Value = "Permanent Address";
                    dataGridViewUserListing.Columns["PresenAddress"].HeaderCell.Value = "Present Address";
                    dataGridViewUserListing.Columns["Photo"].HeaderCell.Value = "Image";
                    dataGridViewUserListing.Columns["PhoneNumber"].HeaderCell.Value = "Contact #";
                    dataGridViewUserListing.Columns["FatherName"].HeaderCell.Value = "Father Name";
                    dataGridViewUserListing.Columns["MembershipId"].HeaderCell.Value = "Member#";

                }
                catch (Exception e1)
                {


                }
 
            }
            else if (searchUser2.radioButtonBarCode.Checked)
            {
                try
                {
                    string userIdToSearch = searchUser2.textBoxBarCode.Text.Trim().Replace("TECH", "");

                    var listOfUsers = (from suser in dbContext.SecurityUsers
                                       where ((suser.UserId == userIdToSearch)                                       
                                       )
                                       select new
                                       {
                                           suser.id,
                                           suser.UserId,
                                           suser.Name,
                                           suser.NIC,
                                           suser.DOB,
                                           suser.PermAddress,
                                           suser.PresenAddress,
                                           suser.Photo,
                                           suser.ThumbImpression,
                                           suser.BarCode,
                                           suser.IsActive,
                                           suser.PhoneNumber,
                                           suser.RoleId,
                                           suser.FatherName,
                                           suser.DOE,
                                           suser.IsResident,
                                           suser.MembershipId,
                                           suser.RoleDesId,
                                           suser.OtherDesc,
                                           suser.ThumbImage
                                       }).ToList();
                    dataGridViewUserListing.DataSource = listOfUsers;

                    dataGridViewUserListing.Columns[0].Visible = false;

                    dataGridViewUserListing.Columns["id"].HeaderCell.Value = "ID";
                    dataGridViewUserListing.Columns["UserId"].HeaderCell.Value = "User ID";
                    dataGridViewUserListing.Columns["Name"].HeaderCell.Value = "Name";
                    dataGridViewUserListing.Columns["NIC"].HeaderCell.Value = "NIC";
                    dataGridViewUserListing.Columns["DOB"].HeaderCell.Value = "DOB";
                    dataGridViewUserListing.Columns["PermAddress"].HeaderCell.Value = "Permanent Address";
                    dataGridViewUserListing.Columns["PresenAddress"].HeaderCell.Value = "Present Address";
                    dataGridViewUserListing.Columns["Photo"].HeaderCell.Value = "Image";
                    dataGridViewUserListing.Columns["PhoneNumber"].HeaderCell.Value = "Contact #";
                    dataGridViewUserListing.Columns["FatherName"].HeaderCell.Value = "Father Name";
                    dataGridViewUserListing.Columns["MembershipId"].HeaderCell.Value = "Member#";

                }
                catch (Exception e1)
                {


                }


            }
            else 
            {

            }
        }
     
    }
}
